<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 李邦琦 <1536549251@qq.com>
// +----------------------------------------------------------------------

namespace app\crm\model;
use think\Db;
use think\Model;
use think\Cache;

class File extends Model
{
    /**
     * 获取公告列表
     * @param str $where <查询条件>
     * @param int $page <页码>
     * @param int $limit <每页显示条数>
     * @return array <日报列表信息>
     */
    public function getFileData($where,$page,$limit)
    {
        $count = Db::name('mob_faq_content')->where($where)->count();
        // 查询状态为1的用户数据 并且每页显示10条数据
        $fileList = Db::name('mob_faq_content')->alias('c')->join('xy_mob_faq_type t','c.type_id= t.id')->field('c.*, t.name tname')->where($where)->limit(($page-1)*$limit,$limit)->order('c.createtime DESC')->select();
        $users = Cache::get('users');
        foreach ($fileList as &$val) {
            $val['file_content'] = strip_tags($val['content']);
            if ($val['file_path']) {
                $val['file_download'] = '<a class="layui-btn layui-btn-danger layui-btn-mini" href="'.$val['file_path'].'" download="'.$val['title'].'">下载</a>';
            } else {
                $val['file_download'] = '<a class="layui-btn layui-btn-primary layui-btn-mini">无附件</a>';
            }
            $val['create_user'] = $users[$val['create_user']]['cn_name'];
        }
        $rowsList = count($fileList) ? $fileList : false;
        $arrData["code"] = 0;
        $arrData["msg"] = '';
        $arrData["count"] = $count;
        $arrData["data"] = $rowsList;
        return $arrData;
    }


    /**
     * 获取单个文件的信息
     * @param  str $id [文件ID]
     * @return arr     [文件信息]
     */
    public function getFileInfo($id)
    {
        // $id = 27;
        $fileInfo = Db::name('mob_faq_content')->where('id',$id)->find();
        return $fileInfo;
    }


    /**
     * 获取文件评论
     * @param  [string] $daily_id [文件ID]
     * @return [arr]              [文件评论列表/空数组]
     */
    public function getFileComment($faq_id)
    {
        $fileCommentInfo = Db::name('mob_faq_operating_record')->where('faq_id',$faq_id)->select();
        foreach ($fileCommentInfo as &$val) {
            $val['create_user'] = empty($users[$val['create_user']]['cn_name'])? ' ':$users[$val['create_user']]['cn_name'];
        }
        return $fileCommentInfo;
    }

}